package io.coderyeah.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.coderyeah.basic.Result;
import io.coderyeah.basic.util.LoginContext;
import io.coderyeah.order.domain.OrderPetAcquisition;
import io.coderyeah.order.dto.OrderDTO;
import io.coderyeah.order.mapper.OrderPetAcquisitionMapper;
import io.coderyeah.order.service.OrderPetAcquisitionService;
import io.coderyeah.org.domain.BaseDomain;
import io.coderyeah.org.domain.Employee;
import io.coderyeah.org.domain.Shop;
import io.coderyeah.org.mapper.EmployeeMapper;
import io.coderyeah.org.mapper.ShopMapper;
import io.coderyeah.pet.domain.Pet;
import io.coderyeah.pet.mapper.PetMapper;
import io.coderyeah.search.domain.SearchMasterMsg;
import io.coderyeah.search.mapper.SearchMasterMsgMapper;
import io.coderyeah.search.service.SearchMasterMsgService;
import io.coderyeah.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author coderyeah
 * @description 针对表【t_order_pet_acquisition】的数据库操作Service实现
 * @createDate 2022-09-29 13:21:51
 */
@Service
public class OrderPetAcquisitionServiceImpl extends ServiceImpl<OrderPetAcquisitionMapper, OrderPetAcquisition> implements OrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper acquisitionMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Resource
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Resource
    private PetMapper petMapper;
    @Autowired
    private SearchMasterMsgService masterMsgService;

    @Override
    public Result getAll(OrderDTO orderDTO) {
        final List<OrderPetAcquisition> list = acquisitionMapper.selectList(null);
        final Integer totals = acquisitionMapper.selectCount(null);
        return Result.success(list, (long) totals);
    }

    @Override
    public Result getAll(OrderDTO orderDTO, HttpServletRequest request) {
        // 获取当前登录管理员
        final LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        assert loginInfo != null;
        final Employee employee = employeeMapper.selectOne(new LambdaQueryWrapper<Employee>().eq(Employee::getLogininfoId, loginInfo.getId()));
        final Long shopId = employee.getShopId();
        if (null != shopId) {
            final Shop shop = shopMapper.selectOne(new LambdaQueryWrapper<Shop>().eq(BaseDomain::getId, shopId));
            if (shop.getAdminId() != employee.getId()) {
                // 表示店铺管理员
                orderDTO.setEmpId(employee.getId());
            }
            // 店铺超管
            orderDTO.setShopId(shopId);
        }
        // 平台管理员查询所有订单
        List<OrderPetAcquisition> acquisitions = acquisitionMapper.getOrders(orderDTO);
        final Integer totals = acquisitionMapper.selectCount(null);
        return Result.success(acquisitions, (long) totals);
    }

    @Transactional
    @Override
    public Result confirm(OrderDTO orderDTO) {
        // 修改订单状态
        final OrderPetAcquisition order = acquisitionMapper.selectById(orderDTO.getId());
        order.setState(1);
        order.setPrice(orderDTO.getMoney());
        order.setPaytype(order.getPaytype());
        acquisitionMapper.updateById(order);
        // 修改寻主消息状态 1待处理 -2已完成
        final Long msgId = order.getSearchMasterMsgId();
        final SearchMasterMsg masterMsg = searchMasterMsgMapper.selectById(msgId);
        masterMsg.setState(2);
        searchMasterMsgMapper.updateById(masterMsg);
        // 修改pet的成本价
        final Long petId = order.getPetId();
        final Pet pet = petMapper.selectById(petId);
        pet.setCostprice(orderDTO.getMoney());
        petMapper.updateById(pet);
        return Result.success(null);
    }

    @Transactional
    @Override
    public Result cancel(Long id) {// 订单id
        final OrderPetAcquisition order = acquisitionMapper.selectById(id);
        // 寻主消息shopId置空
        masterMsgService.reject(order.getSearchMasterMsgId());
        // 删除宠物信息 - 订单取消了，宠物就没有接回来。之前已经将宠物录入系统了
        petMapper.deleteById(order.getPetId());
        // 修改订单状态
        order.setState(-1);
        acquisitionMapper.updateById(order);
        return Result.success(null);
    }
}




